package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpExprMapper empExprMapperp;

    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        //1. 设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

        //2. 执行查询
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) empList;
        log.info("分页信息：{}", p);
        //3. 封装结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Transactional
    @Override
    public void add(Emp emp) {
        //1.补全基础属性(创建时间 更新时间)
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(emp.getCreateTime());

        //2.保存员工基本信息
        empMapper.insert(emp);

        //3.实现批量保存员工的工作经历信息
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }
    /**
     * 查询所有员工
     * @return
     */
    @Override
    public List<Emp> list() {
        List<Emp> empList = empMapper.selectAll();
        return empList;
    }

    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        //查询员工
        Emp emp = empMapper.get(id);
        //查询员工对应的工作经历
        List<EmpExpr> exprList = empExprMapperp.getByEmpId(id);
        emp.setExprList(exprList);
        return emp;
    }

    @Transactional
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getByUsernameAndPassword(emp);
        if(empLogin != null){
            Map<String,Object> clamis = new HashMap<>();
            clamis.put("id",empLogin.getId());
            clamis.put("username",empLogin.getUsername());
            String jwt = JwtUtils.generateJwt(clamis);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(),jwt);
            return loginInfo;
        }
        return  null;
    }
}
